package com.banciyuan.bcywebview.biz.circles.discover.tagnav;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.daoexample.model.FollowTag;


/**
 * Created by squall on 2017/3/20.
 */

public class DiscoverTagNavLeftAdapter extends BaseAdapter {

    private List<FollowTag> slist = new ArrayList<>();
    private HashMap<String, Boolean> map = new HashMap<>();
    private Context mContext;
    private String pre_name;
    private DiscoverTagNavFragment.OnDataChange onDataChange;

    public DiscoverTagNavLeftAdapter(Context mContext, List<FollowTag> slist, DiscoverTagNavFragment.OnDataChange onDataChange) {
        this.mContext = mContext;
        this.slist = slist;
        this.onDataChange = onDataChange;
        for (FollowTag s : slist) {
            if (slist.indexOf(s) == 0) {
                map.put(s.getTag_name(), true);
            } else {
                map.put(s.getTag_name(), false);
            }
        }
    }

    @Override
    public int getCount() {
        return slist.size();
    }

    @Override
    public Object getItem(int position) {
        return slist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemHolder itemHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.discover_tag_left_item, null);
            itemHolder = new ItemHolder(convertView);
            convertView.setTag(itemHolder);
        } else {
            itemHolder = (ItemHolder) convertView.getTag();
        }
        final FollowTag f = slist.get(position);
        if (map.get(f.getTag_name())) {
            itemHolder.tag_name.setSelected(true);
            ObjectAnimator.ofFloat(itemHolder.indictor, "scaleY", 1f).setDuration(100).start();
            pre_name = f.getTag_name();
        } else {
            itemHolder.tag_name.setSelected(false);
            itemHolder.indictor.setScaleY(0f);
        }
        itemHolder.tag_name.setText(f.getTag_name());
        itemHolder.tag_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onDataChange.onChange(f);
                map.put(pre_name, false);
                map.put(f.getTag_name(), true);
                v.setSelected(!v.isSelected());
                notifyDataSetChanged();
            }
        });

        return convertView;
    }

    private class ItemHolder {

        private TextView tag_name;
        private View indictor;

        public ItemHolder(View itemView) {
            tag_name = (TextView) itemView.findViewById(R.id.tag_name);
            indictor = itemView.findViewById(R.id.indictor);
        }
    }
}
